
from apps.pay.order.dao import OrderDao
from apps.chat.message.functions import MessageFun
from apps.pay.recordIncomes.functions import RecordIncomesFun
import json
from apps.merchant.sku.dao import SkuDao

class OrderFun:

    def changeOrderStatus(self, order_id, orderStatus):
        order = OrderDao().findOneByObjId(order_id)
        if orderStatus == 'CANCEL':
            data = {'orderStatus': orderStatus}
            OrderDao().update(order.id, data)
            # 返还库存
            content = json.loads(order.content)
            for item in content.get('orderItemList'):
                if item.get('sku'):
                    SkuDao().addStock(item.get('sku'), item.get('payCount'))
            MessageFun().sendMessage(None, order.user, 'jytz', {'title': '订单通知', 'content': '订单已取消'})
        elif orderStatus == 'WAIT_SEND':
            data = {'orderStatus': orderStatus}
            OrderDao().update(order.id, data)
            if order.rider:
                MessageFun().sendMessage(None, order.rider, 'system', {'title': '任务通知', 'content': '您有1个新增待取件任务'})
            if order.user:
                if order.addrinfo:
                    addrinfoDict = json.loads(order.addrinfo)
                    print('addrinfoDict', addrinfoDict)
                    MessageFun().sendMessage(None, order.user, 'jytz', {'title': '订单通知', 'content': '您已支付成功，收货地址为'+addrinfoDict.get('addrInfo')})
        elif orderStatus == 'WAIT_RECEIVE':
            data = {'orderStatus': orderStatus}
            OrderDao().update(order.id, data)
            MessageFun().sendMessage(None, order.user, 'jytz', {'title': '订单通知', 'content': '您的快件正在配送中'})
        elif orderStatus == 'FINISH':
            data = {'orderStatus': orderStatus}
            OrderDao().update(order.id, data)
            MessageFun().sendMessage(None, order.user, 'jytz', {'title': '订单通知', 'content': '您的快件已送达'})
            # 给骑手分成
